package exam.axhu.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import exam.axhu.entity.Admin;
import exam.axhu.service.AdminService;


@Service @Transactional
public class AdminServiceImpl implements AdminService{
//defaltBean: adminServiceImpl
	@Resource SessionFactory factory;
	
	public int save(Admin admin) {
		
			factory.getCurrentSession().persist(admin);
			return 0;   //成功保存用户
	}
	

	public void delete(String... adminIDs) {
		for(String adminID : adminIDs){
			if(this.findByID(adminID)!=null)
				factory.getCurrentSession().delete(factory.getCurrentSession().load(Admin.class,adminID));
		}
		
	}


	public void update(Admin admin) {
		factory.getCurrentSession().merge(admin);
		
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Admin findByID(String adminID) {
		return (Admin) factory.getCurrentSession().get(Admin.class,adminID);
	}
	
	
	@SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<Admin> findByName(String name){
			//条件查询，自定义索引名(参数名):username,:password.通过setString,setParameter设置参数   
			Query query = factory.getCurrentSession().createQuery("from Admin as c where c.name = :searchName ");
			query.setString("searchName", name);
			return query.list();
	        
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<Admin> list() {
		return factory.getCurrentSession().createQuery("from Admin").list();
	}



}
